package org.cht.dao.hibernate;

import org.cht.dao.DeveloperDao;
import org.cht.model.Developer;
import org.cht.model.Technology;

import java.util.List;

/**
 * Copyright (C) 2007 Cape Henry Technologies Inc.
 * <p/>
 * This program is open software. It is licensed using the Apache Software
 * Foundation, version 2.0 January 2004
 * <p/>
 * <a href="mailto:dlwhitehurst@gmail.com">dlwhitehurst@gmail.com</a>
 *
 * @author David L. Whitehurst
 * @version 1.0
 */
public class DeveloperDaoHibernate extends GenericDaoHibernate<Developer, Long> implements DeveloperDao {

    /**
     * Constructor that sets the entity to Developer.class.
     */
    public DeveloperDaoHibernate() {
        super(Developer.class);
    }

    /**
     * Gets the complete list of developers ordered by name.
     *
     * @return List populated list of developers
     */
    @SuppressWarnings("unchecked")
    public List<Developer> getDevelopers() {
        return getHibernateTemplate().find("from Developer d order by upper (d.name)");
    }

    /**
     * Gets the list of developers by technology (key)
     *
     * @param name
     * @return List populated list of developers
     */
    @SuppressWarnings("unchecked")
    public List<Developer> getDevelopersByTechnology(String name) {

        return getHibernateTemplate().find("from Developer as d inner join d.technologies as techie with techie.name = '" + name + "'");
    }

    /**
     * Saves a developer's information.
     *
     * @param developer the object to be saved
     * @return the persisted Developer object
     */
    public Developer saveDeveloper(Developer developer) {
        log.debug("developer's id: " + developer.getId());
        getHibernateTemplate().saveOrUpdate(developer);
        // necessary to throw a DataIntegrityViolation and catch it in DeveloperManager
        getHibernateTemplate().flush();
        return developer;
    }
}
